﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using LogisticsBLL;
namespace LogisticsManagementSystem.Truck
{
    public partial class TruckManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {


                //统计总条数
                lblCount.Text = TruckBLL.GetTruckCount().ToString();


                rptTruckList.DataSource = TruckBLL.GetTruckList(0,null,null,0);
                rptTruckList.DataBind();


                ddlTeamNameList.DataSource = TruckBLL.GetTruckName();          
                ddlTeamNameList.DataTextField = "TeamName";
                ddlTeamNameList.DataValueField = "TeamID";
                ddlTeamNameList.DataBind();
                ddlTeamNameList.Items.Insert(0, new ListItem("全部", "0"));


                ddlTruckType.DataSource = TruckBLL.GetTruckType();
                ddlTruckType.DataTextField = "Type";
                ddlTruckType.DataValueField = "Type";
                ddlTruckType.DataBind();
                ddlTruckType.Items.Insert(0, new ListItem("全部", "0"));


                ddlTruckState.DataTextField = "State";
                ddlTruckState.DataValueField = "TruckID";
                ddlTruckState.DataBind();
                ddlTruckState.Items.Insert(0, new ListItem("全部", "0"));

                


            }
        }

        protected void rptTruckList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName== "delTruck")
            {
                int tid = Convert.ToInt32(e.CommandArgument);
                TruckBLL.DeleteTruck(tid);
                rptTruckList.DataSource = LogisticsBLL.TruckBLL.GetTruckList(0, null,null, 0);
                rptTruckList.DataBind();
                lblCount.Text = LogisticsBLL.TruckBLL.GetTruckCount().ToString();
            }
            else
            {   
              
                Response.Redirect("EditTruck.aspx?id="+e.CommandArgument);

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int teamId = Convert.ToInt32(ddlTeamNameList.SelectedValue);
            string number = txtNumber.Value;
            string type = ddlTruckType.SelectedValue;
            int state = Convert.ToInt32(ddlTruckState.SelectedValue);
            List<LogisticsModel.Truck> trucks = LogisticsBLL.TruckBLL.GetTruckList(teamId, number, type, state);
            rptTruckList.DataSource = trucks;
            rptTruckList.DataBind();
            lblCount.Text = trucks.Count.ToString();

        }

        protected void rptTruckList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LogisticsModel.Truck state = (LogisticsModel.Truck)e.Item.DataItem;
                HtmlTableCell tableCell = e.Item.FindControl("tbState") as HtmlTableCell;
                switch (state.State)
                {
                    case 1:
                        tableCell.InnerHtml = "<font color='red'>承运中</font>";
                        break;
                    case 2:
                        tableCell.InnerHtml = "<font color='green'>空闲中</font>";
                        break;
                }

            }
        }
    }
}